//
//  ViewControllerTwo.m
//  10-20
//
//  Created by mac on 16/10/24.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewControllerTwo.h"
#import "LoginData.h"
#import "ListViewController.h"
@interface ViewControllerTwo ()
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *passworld;

@end

@implementation ViewControllerTwo
- (IBAction)onClick:(id)sender
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/login.php?name=%@&password=%@",self.name.text,self.passworld.text]];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *take = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSString *text = [[NSString alloc] initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
        NSLog(@"%@",text);
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        LoginData *d = [[LoginData alloc] initWithDictionary:dict];
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if(d.errcode == 0)
            {
                ListViewController *viewcont = [self.storyboard instantiateViewControllerWithIdentifier:@"list"];
                [self.navigationController pushViewController:viewcont animated:YES];
            }
            else
            {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil]];
                [self presentViewController:alert animated:YES completion:nil];
            }
        });
    }];
    [take resume];

}
- (IBAction)openKeyboard:(id)sender
{
    [self.name becomeFirstResponder];
}

- (IBAction)closeKeyboard:(id)sender
{
    //[self.name resignFirstResponder];
    //[self.passworld resignFirstResponder];
    [self.name endEditing:YES];
}

- (void)endEdit
{
    [self.name endEditing:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.name.text = @"test";
    self.passworld.text = @"123";
    self.passworld.secureTextEntry = YES;
    
    UIBarButtonItem *close = [[UIBarButtonItem alloc] initWithTitle:@"收起键盘" style:UIBarButtonItemStylePlain target:self action:@selector(endEdit)];
    
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *space2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIToolbar *tool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    tool.items = @[space,close,space2];
    self.name.inputAccessoryView = tool;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
